/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkErrorCode.h"
#include "MkUtil.h"
#include "MkLog.h"

CMkErrorCode CMkErrorCode::m_Instance;
CMkErrorCode::CMkErrorCode()
{
    ErrorCodeDesc Desc;
    Desc.CnDesc = "成功";
    Desc.CnTreatmentMethod = "无";
    Desc.EnTreatmentMethod = "None";
    Desc.EnDesc = "success";
    Desc.ErrorCode = NoneError;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "创建文件夹失败";
    Desc.CnTreatmentMethod = "查看是否拥有创建文件夹权限";
    Desc.EnTreatmentMethod = "check whether you have create folder permission";
    Desc.EnDesc = "create folder failed";
    Desc.ErrorCode = CreateFolderFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "队列已经满";
    Desc.CnTreatmentMethod = "请联系技术支持";
    Desc.EnTreatmentMethod = "please contact technical support";
    Desc.EnDesc = "queue is full";
    Desc.ErrorCode = QueueFull;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "队列为空";
    Desc.CnTreatmentMethod = "请联系技术支持";
    Desc.EnTreatmentMethod = "please contact technical support";
    Desc.EnDesc = "queue is empty";
    Desc.ErrorCode = QueueEmpty;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "队列已经销毁";
    Desc.CnTreatmentMethod = "请联系技术支持";
    Desc.EnTreatmentMethod = "please contact technical support";
    Desc.EnDesc = "queue is uninted";
    Desc.ErrorCode = QueueUinited;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "创建线程失败";
    Desc.CnTreatmentMethod = "检查内存是否已经被用完";
    Desc.EnTreatmentMethod = "check whether the memeroy is all used";
    Desc.EnDesc = "create thread failed";
    Desc.ErrorCode = CreateThreadFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;
    Desc.CnDesc = "打开文件失败";
    Desc.CnTreatmentMethod = "查看是否拥有打开文件权限";
    Desc.EnTreatmentMethod = "check whether you have open file permission";
    Desc.EnDesc = "open file failed";
    Desc.ErrorCode = OpenFileFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "打开文件夹失败";
    Desc.CnTreatmentMethod = "查看是否拥有打开文件夹权限";
    Desc.EnTreatmentMethod = "check whether you have open folder permission";
    Desc.EnDesc = "open folder failed\n";
    Desc.ErrorCode = OpenFloderFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "无效的Url";
    Desc.CnTreatmentMethod = "请传入正确的URL";
    Desc.EnTreatmentMethod = "please input correct url";
    Desc.EnDesc = "invalid url";
    Desc.ErrorCode = InvalidUrl;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "socket连接失败";
    Desc.CnTreatmentMethod = "检查目的地网络是否正常";
    Desc.EnTreatmentMethod = "please check destination network is abnormal";
    Desc.EnDesc = "connect socket failed\n";
    Desc.ErrorCode = SocketConnectFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "监听端口失败";
    Desc.CnTreatmentMethod = "检查端口是否已经被另外程序使用";
    Desc.EnTreatmentMethod = "please check the listen port is used by other application";
    Desc.EnDesc = "socket listen failed";
    Desc.ErrorCode = SocketListenFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "无效时间";
    Desc.CnTreatmentMethod = "请传入正确的时间格式";
    Desc.EnTreatmentMethod = "please input correct time string";
    Desc.EnDesc = "invalid time string";
    Desc.ErrorCode = InvalidTime;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "无效参数";
    Desc.CnTreatmentMethod = "请传入正确的请求格式";
    Desc.EnTreatmentMethod = "please input correct params from restapi doc";
    Desc.EnDesc = "invalid params";
    Desc.ErrorCode = InvalidParams;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "未知错误";
    Desc.CnTreatmentMethod = "请联系技术支持";
    Desc.EnTreatmentMethod = "please contact technical support";
    Desc.EnDesc = "unknown error";
    Desc.ErrorCode = UnknownError;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "申请内存失败";
    Desc.CnTreatmentMethod = "检查内存是否已经被用完";
    Desc.EnTreatmentMethod = "check whether the memeroy is all used";
    Desc.EnDesc = "malloc buffer failed";
    Desc.ErrorCode = MallocFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "事件超时";
    Desc.CnTreatmentMethod = "检查访问对象是否有效";
    Desc.EnTreatmentMethod = "check whether the access object is valid";
    Desc.EnDesc = "time out";
    Desc.ErrorCode = TimeOut;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "id已经被使用了";
    Desc.CnTreatmentMethod = "请传入未使用的id";
    Desc.EnTreatmentMethod = "please input unused id";
    Desc.EnDesc = "id is used";
    Desc.ErrorCode = IdIsUsed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "回调服务器未设置";
    Desc.CnTreatmentMethod = "通过接口设置回调服务器地址";
    Desc.EnTreatmentMethod = "please set callback server from restapi";
    Desc.EnDesc = "callback server not set";
    Desc.ErrorCode = CallbackSvrNotSet;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "创建socket失败";
    Desc.CnTreatmentMethod = "检查打开文件个数限制太小";
    Desc.EnTreatmentMethod = "please check the open file limited is to small";
    Desc.EnDesc = "socket create failed";
    Desc.ErrorCode = SocketCreateFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "发送socket数据失败";
    Desc.CnTreatmentMethod = "检查网络是否异常";
    Desc.EnTreatmentMethod = "please check network is abnormal";
    Desc.EnDesc = "socket send buffer failed";
    Desc.ErrorCode = SocketSendFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "发送socket到缓存";
    Desc.CnTreatmentMethod = "发送数据量大或者还未成功连接服务器，系统将稍后发送";
    Desc.EnTreatmentMethod = "not connected or send too more,system cache the buffer,will send for a moument";
    Desc.EnDesc = "send buffer to cache";
    Desc.ErrorCode = SocketSendCached;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "接收socket数据失败";
    Desc.CnTreatmentMethod = "检查网络是否异常";
    Desc.EnTreatmentMethod = "please check network is abnormal";
    Desc.EnDesc = "socket recv buffer failed";
    Desc.ErrorCode = SocketRecvFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "socket绑定端口失败";
    Desc.CnTreatmentMethod = "检查端口是否已经被另外程序使用";
    Desc.EnTreatmentMethod = "please check the listen port is used by other application";
    Desc.EnDesc = "socket bind port failed";
    Desc.ErrorCode = SocketBindFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "接受socket连接失败";
    Desc.CnTreatmentMethod = "检查打开文件个数限制太小";
    Desc.EnTreatmentMethod = "please check the open file limited is to small";
    Desc.EnDesc = "socket accept failed";
    Desc.ErrorCode = SocketAcceptFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "设置socket opt属性失败";
    Desc.CnTreatmentMethod = "检查socket是否有效";
    Desc.EnTreatmentMethod = "please check the socket is invalid";
    Desc.EnDesc = "socket set opt failed";
    Desc.ErrorCode = SocketSetOptFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "设置socket ioctl属性失败";
    Desc.CnTreatmentMethod = "检查socket是否有效";
    Desc.EnTreatmentMethod = "please check the socket is invalid";
    Desc.EnDesc = "socket set ioctl failed";
    Desc.ErrorCode = SocketIoctlFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "无效的id";
    Desc.CnTreatmentMethod = "请传入正确的id";
    Desc.EnTreatmentMethod = "please input correct id string";
    Desc.EnDesc = "invalid id string";
    Desc.ErrorCode = InvalidId;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "无效的会话";
    Desc.CnTreatmentMethod = "请传入正确的会话";
    Desc.EnTreatmentMethod = "please input correct session string";
    Desc.EnDesc = "invalid session string";
    Desc.ErrorCode = InvalidSession;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "未初始化";
    Desc.CnTreatmentMethod = "请初始化后再操作";
    Desc.EnTreatmentMethod = "please init first";
    Desc.EnDesc = "not init";
    Desc.ErrorCode = UnInited;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "不支持该功能";
    Desc.CnTreatmentMethod = "系统不支持该功能";
    Desc.EnTreatmentMethod = "system not support";
    Desc.EnDesc = "system not support";
    Desc.ErrorCode = UnSupported;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "Rtmp库返回失败";
    Desc.CnTreatmentMethod = "检查Rtmp服务器是否正常";
    Desc.EnTreatmentMethod = "please check rtmp server is correct";
    Desc.EnDesc = "visit rtmp server failed";
    Desc.ErrorCode = RtmpError;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "没有足够的缓存";
    Desc.CnTreatmentMethod = "等待更多缓存再处理";
    Desc.EnTreatmentMethod = "wait for enough buffer";
    Desc.EnDesc = "not has enough buffer";
    Desc.ErrorCode = NotHasEnoughBuffer;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "协议不支持";
    Desc.CnTreatmentMethod = "系统不支持该协议";
    Desc.EnTreatmentMethod = "system not support this protocol";
    Desc.EnDesc = "protocol un suppport";
    Desc.ErrorCode = UnSupportProtocol;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "Xml解析未找到Attribute";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml node not has attribute";
    Desc.ErrorCode = XmlErrorNoAttribute;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "Xml Attribute 类型错误";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml attribute type wrong";
    Desc.ErrorCode = XmlErrorWrongAttributeType;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 文件未找到";
    Desc.CnTreatmentMethod = "输入正确xml文件路径";
    Desc.EnTreatmentMethod = "input correct xml path";
    Desc.EnDesc = "xml file not found";
    Desc.ErrorCode = XmlErrorFileNotFound;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml文件未被打开";
    Desc.CnTreatmentMethod = "先打开文件后操作";
    Desc.EnTreatmentMethod = "open xml file first";
    Desc.EnDesc = "xml file could not be opened";
    Desc.ErrorCode = XmlErrorFileCouldNotBeOpened;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "读xml文件失败";
    Desc.CnTreatmentMethod = "检查文件权限";
    Desc.EnTreatmentMethod = "check file module";
    Desc.EnDesc = "xml file read error";
    Desc.ErrorCode = XmlErrorFileReadError;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 解析节点失败";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml parse element error";
    Desc.ErrorCode = XmlErrorParsingElement;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 解析 attribute 失败";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml parse attribute error";
    Desc.ErrorCode = XmlErrorParsingAttribute;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 解析文本失败";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml parse text error";
    Desc.ErrorCode = XmlErrorParsingText;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 解析 cdata 失败";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml parse cdata error";
    Desc.ErrorCode = XmlErrorParsingCdata;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 解析 comment 失败";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml parse comment error";
    Desc.ErrorCode = XmlErrorParsingComment;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 解析 declaration 失败";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml parse declaration failed";
    Desc.ErrorCode = XmlErrorParsingDeclaration;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 解析未知错误";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml parse error unknown";
    Desc.ErrorCode = XmlErrorParsingUnknown;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "未找到xml文档";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml parse empty document";
    Desc.ErrorCode = XmlErrorEmptyDocument;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 解析 不匹配的节点";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml parse mismathched element";
    Desc.ErrorCode = XmlErrorMismatchedElement;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 解析错误";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml parse error";
    Desc.ErrorCode = XmlErrorParsing;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 不能包含的文本";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml can not cover test";
    Desc.ErrorCode = XmlErrorCanNotConverText;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 未有文本节点";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml not has text node";
    Desc.ErrorCode = XmlErrorNoTextNode;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 节点深度超出范围";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml element depth exceeded";
    Desc.ErrorCode = XmlErrorElementDepthExceeded;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "xml 解析错误";
    Desc.CnTreatmentMethod = "传入Xml错误";
    Desc.EnTreatmentMethod = "input xml error";
    Desc.EnDesc = "xml error count";
    Desc.ErrorCode = XmlErrorCount;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "http 请求错误";
    Desc.CnTreatmentMethod = "传入正确的http请求地址";
    Desc.EnTreatmentMethod = "input correct http url";
    Desc.EnDesc = "http request failed";
    Desc.ErrorCode = HttpRequestFailed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "图像为空";
    Desc.CnTreatmentMethod = "传入有效图像";
    Desc.EnTreatmentMethod = "input correct image";
    Desc.EnDesc = "input image is empty";
    Desc.ErrorCode = ImageEmpty;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "系统繁忙";
    Desc.CnTreatmentMethod = "请稍后重试";
    Desc.EnTreatmentMethod = "please try again wait for a moment";
    Desc.EnDesc = "system is busy";
    Desc.ErrorCode = SystemBusy;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "错误码已被使用";
    Desc.CnTreatmentMethod = "请重新定义错误码值";
    Desc.EnTreatmentMethod = "please redefine error code";
    Desc.EnDesc = "error code is used";
    Desc.ErrorCode = ErrorCodeUsed;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "系统错误";
    Desc.CnTreatmentMethod = "检查系统资源是否使用完";
    Desc.EnTreatmentMethod = "please check system resource";
    Desc.EnDesc = "system error";
    Desc.ErrorCode = SystemError;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;

    Desc.CnDesc = "未知错误";
    Desc.CnTreatmentMethod = "未知错误";
    Desc.EnTreatmentMethod = "unknown error";
    Desc.EnDesc = "unknown error";
    Desc.ErrorCode = UnknownError;
    m_MapErrorDesc[Desc.ErrorCode] = Desc;
}

Uint32 CMkErrorCode::GetErrorcodeDesc(Uint32 ErrorCode, ErrorCodeDesc& Desc)
{
    MkUnorderedMap<Uint32, ErrorCodeDesc>::iterator it = m_MapErrorDesc.find(ErrorCode);
    if (it != m_MapErrorDesc.end()) {
        Desc = it->second;
    } else {
        Desc.CnDesc = "错误码未定义";
        Desc.CnTreatmentMethod = "请联系系统管理员";
        Desc.EnDesc = "error code not define";
        Desc.EnTreatmentMethod = "please contact technical support";
    }
    return NoneError;
}

Uint32 CMkErrorCode::AddErrorcodeDesc(const ErrorCodeDesc& Desc)
{
    MkUnorderedMap<Uint32, ErrorCodeDesc>::iterator it = m_MapErrorDesc.find(Desc.ErrorCode);
    if (it != m_MapErrorDesc.end()) {
        MkErrorLog("error code:%d is used\n", Desc.ErrorCode);
        return ErrorCodeUsed;
    }
    m_MapErrorDesc[Desc.ErrorCode] = Desc;
    return NoneError;
}

CMkErrorCode::~CMkErrorCode()
{

}


